#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

string expr;
int n, m;
int a[N], f[N];
struct Node {
  int val;
  char op;
  int ch[2];
} tr[N * 10];
#define val(p) tr[p].val
#define op(p) tr[p].op
#define lc(p) tr[p].ch[0]
#define rc(p) tr[p].ch[1]

int newnode() {
  static int tot = 0;
  return ++tot;
}
int build() {
  rep(i, 1, n) {
    int p = newnode();
    val(p) = a[i];
  }
  stringstream ss(expr);
  stack<int> st;
  char op;
  int id;
  while (ss >> op) {
    if (op == 'x') {
      ss >> id;
      st.push(id);
    } else {
      int p = newnode();
      op(p) = op;
      if (op == '!') {
        int ch = st.top();
        st.pop();
        f[ch] = p;
        lc(p) = ch;
        val(p) = !val(ch);
      } else {
        rc(p) = st.top(), st.pop();
        f[rc(p)] = p;
        lc(p) = st.top(), st.pop();
        f[lc(p)] = p;
        if (op == '&')
          val(p) = val(lc(p)) && val(rc(p));
        else
          val(p) = val(lc(p)) || val(rc(p));
      }
      st.push(p);
    }
  }
  return st.top();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  getline(cin, expr);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  int root = build();
  cin >> m;
  while (m--) {
    int id;
    cin >> id;
    int x = !val(id);
    int p = id;
    while (p != root) {
      int fp = f[p];
      int q = tr[fp].ch[lc(fp) == p];
      if (op(fp) == '!')
        x = !x;
      else if (op(fp) == '&')
        x = x && val(q);
      else if (op(fp) == '|')
        x = x || val(q);
      p = fp;
      if (x == val(p)) break;
    }
    if (x == val(p))
      cout << val(root) << endl;
    else
      cout << x << endl;
  }
  return 0;
}